import {
  CHANGE_MY_NOTE,
  CHANGE_MY_COLLECT_NOTE,
  CHANGE_OTHER_USER_INFO,
} from "./constant";
// 网络请求
import { getAllMyNote, getCollectNote } from "@/services";
import { getUserById } from "../../../services";

const changeMyNote = (data) => ({
  type: CHANGE_MY_NOTE,
  data,
});
const changeMyCollectNote = (data) => ({
  type: CHANGE_MY_COLLECT_NOTE,
  data,
});

export const getOtherUserInfoAction = (userId) => {
  return (dispatch) => {
    const notePromise = getAllMyNote(userId);
    const collectNotePromise = getCollectNote(userId);
    Promise.all([notePromise, collectNotePromise]).then((res) => {
      dispatch(changeMyNote(res[0].data.list));
      dispatch(changeMyCollectNote(res[1].data.list));
    });
  };
};

const changeOtherUserInfoAction = (data) => ({
  type: CHANGE_OTHER_USER_INFO,
  data,
});

export const setOtherUserInfoAction = (data) => {
  return (dispatch) => {
    dispatch(changeOtherUserInfoAction(data));
  };
};

// 点击card进入detail页面的时候 去拿到userInfo
export const setAsyncOtherUserInfoAction = (userId) => {
  return (dispath) => {
    getUserById(userId).then((res) => {
      dispath(changeOtherUserInfoAction(res));
    });
  };
};
